package com.luyuan.crm.user.support;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.luyuan.crm.user.Party;
import com.luyuan.crm.user.service.PartyService;
import com.ruijie.components.security.model.AppUser;
import com.ruijie.components.security.service.AppUserService;
import com.ruijie.foundation.dao.extend.support.HibernateEntityExtendDao;
import com.ruijie.foundation.query.Page;
import com.ruijie.foundation.query.criteria.CriteriaQueryTemplate;
import com.ruijie.foundation.query.criteria.CriteriaTemplate;

public class PartyServiceImpl extends HibernateEntityExtendDao<Party> implements PartyService {
    private AppUserService appUserService;

    @Override
    public Party getByAppUser(AppUser appUser) {
        return findUniqueBy("appUser", appUser);
    }

    @Override
    public Party getById(String id) {
        return safeGet(id);
    }

    @Override
    public Party getByUserName(String userName) {
        return findUniqueBy("userName", userName);
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Party> findByRoleName(String[] roleName) {
        Criteria criteria = createCriteria();
        criteria.createCriteria("appUser", "user");
        criteria.createCriteria("user.roles", "role");
        criteria.add(Restrictions.in("role.roleName", roleName));
        return criteria.list();
    }

    @Override
    public void addParty(Party party) {
        AppUser appUser = party.getAppUser();
        appUserService.addUser(appUser);
        flush();

        appUser = appUserService.findUserByLoginName(appUser.getUsername());
        party.setAppUser(appUser);
        party.setCreateTime(new Date());
        save(party);
    }

    @Override
    public void removeParty(Party party) {
        appUserService.removeByIds(new Serializable[] { party.getAppUser().getId() });
        remove(party);
    }

    @Override
    public void disableParty(Party party) {
        AppUser appUser = appUserService.getUserById(party.getAppUser().getId());
        evit(appUser);
        appUser.setPassword(null);
        appUser.setEnabled(false);
        appUserService.update(appUser);
    }

    @Override
    public void enableParty(Party party) {
        AppUser appUser = appUserService.getUserById(party.getAppUser().getId());
        evit(appUser);
        appUser.setPassword(null);
        appUser.setEnabled(true);
        appUserService.update(appUser);
    }

    @Override
    public void updateParty(Party party) {
        party.getAppUser().setPassword(null);
        appUserService.update(party.getAppUser());
        update(party);
    }

    @Override
    public void updatePassword(String loginName, String password) {
        AppUser appUser = appUserService.findUserByLoginName(loginName);
        appUser.setPassword(password);
        appUserService.update(appUser);
    }

    @Override
    public Page pagedQuery(CriteriaTemplate criteria, int pageNo, int pageSize, List<Order> orders) {
        CriteriaQueryTemplate qt = new CriteriaQueryTemplate(getSessionFactory());
        return qt.pagedQuery(criteria, pageNo, pageSize, orders);
    }

    public void setAppUserService(AppUserService appUserService) {
        this.appUserService = appUserService;
    }
}
